import json
import re
import sys
sys.path.append("/home/sivan/0_0/projects/gitee/Small_Tools/utils")
from request import Request
sys.path.append("/home/sivan/0_0/projects/gitee/Small_Tools/db_opr/python")
from mysql import Mysql

class Main:
    def __init__(self, tag):
        self.tag = tag
        db_info = self.__get_conf("/home/sivan/0_0/key_info/mysql_sivan.json")
        self.db = Mysql(db_info)
        self.qqmail_info = self.__get_conf("/home/sivan/0_0/key_info/qqmail/qqmail.json")
        bi = self.qqmail_info
        self.url = "https://mail.qq.com/cgi-bin/note_list"
        m = Request.get(self.url, bi["url_params_begin"], bi["headers"], bi["cookie"])
        #a = re.findall(r'(?<=np_OnReadListItem\(\').*?(?=\')', m, re.M|re.S)
        # a = re.findall(r'(?<=np_ReadNote\(\').*?(?=\')', m, re.M|re.S)
        #print(a)
        #with open("data.txt", "w+") as f:
            #f.write(m)
        count_str = re.findall(r'(?<=All Notes<span>\().*?(?=\))', m, re.M|re.S)[0]
        print(count_str)
        count = int(count_str)
        print("0: ", end="")
        self.get_sub_page(m)
        for i in range(1, count // 25 + 1):
            bi["url_params"]["page"] = str(i)
            m = Request.get(self.url, bi["url_params"], bi["headers"], bi["cookie"])
            print(f"{i}: ", end="")
            self.get_sub_page(m)
    def get_sub_page(self, m):
        nplist = re.findall(r'(?<=<table class="i M pointer" cellspacing="0">).*?(?=</table>)', m, re.M|re.S)
        print(len(nplist))
        np_arr = []
        for np in nplist:
            title = ""
            title_str = re.findall(r'(?<=<b class="black normal">).*?(?=</b>)', np, re.M|re.S)
            if(len(title_str) > 0):
                title = self.__handle_value(title_str[0])
            content = ""
            content_str = re.findall(r'(?<=<span class="graytext">- ).*?(?=</span>)', np, re.M|re.S)
            if (len(content_str) > 0):
                content = self.__handle_value(content_str[0])
            item = {"title": title, "content": content, "all": title + " " + content}
            np_arr.append(item)
        if 0 != len(np_arr): self.__save_db(np_arr)
    def __handle_value(self, value):
        value = value.replace("&nbsp;", " ")
        value = value.replace("'", "\\'")
        value = value.replace("\\", "\\\\")
        return value
    def __save_db(self, np_arr):
        sql = "insert into qqmailnp(title, content, `all`, tag) values \n"
        for i in np_arr:
            sql += f"('{i['title']}', '{i['content']}', '{i['all']}', '{self.tag}'),\n"
        sql = sql[:-2] + ";"
        self.sql = sql
        status = self.db.exec(sql)
        if not status["status"]:
            print(status)
            print(sql)
            raise Exception(str(status))
    def __get_conf(self, file):
        with open(file) as f:
            src = f.read()
        return json.loads(src)

if __name__ == '__main__':
    Main("2022-02-02")


